package com.xique.door.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xique.common.core.text.Convert;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.dao.BuildingDeviceDAO;
import com.xique.door.bean.domain.BuildingDevice;
import com.xique.door.bean.search.build.BuildingDeviceListSearch;
import com.xique.door.mapper.BuildingDeviceMapper;
import com.xique.door.service.IBuildingDeviceService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: caogq
 * @Date: 2022/1/28 15:40
 * @Description:
 */
@Service
public class BuildingDeviceServiceImpl implements IBuildingDeviceService {
    @Autowired
    private BuildingDeviceMapper buildingDeviceMapper;

    /**
     * 查询楼栋设备中间
     *
     * @param id 楼栋设备中间ID
     * @return 楼栋设备中间
     */
    @Override
    public BuildingDevice selectBuildingDeviceById(Long id)
    {
        return buildingDeviceMapper.selectBuildingDeviceById(id);
    }

    /**
     * 查询楼栋设备中间列表
     *
     * @param buildingDevice 楼栋设备中间
     * @return 楼栋设备中间
     */
    @Override
    public List<BuildingDevice> selectBuildingDeviceList(BuildingDevice buildingDevice)
    {
        return buildingDeviceMapper.selectBuildingDeviceList(buildingDevice);
    }

    /**
     * 新增楼栋设备中间
     *
     * @param buildingDevice 楼栋设备中间
     * @return 结果
     */
    @Override
    public int insertBuildingDevice(BuildingDevice buildingDevice)
    {
        buildingDevice.setCreateTime(DateUtils.getNowDate());
        return buildingDeviceMapper.insertBuildingDevice(buildingDevice);
    }

    /**
     * 修改楼栋设备中间
     *
     * @param buildingDevice 楼栋设备中间
     * @return 结果
     */
    @Override
    public int updateBuildingDevice(BuildingDevice buildingDevice)
    {
        buildingDevice.setUpdateTime(DateUtils.getNowDate());
        return buildingDeviceMapper.updateBuildingDevice(buildingDevice);
    }

    /**
     * 删除楼栋设备中间对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBuildingDeviceByIds(String ids)
    {
        return buildingDeviceMapper.deleteBuildingDeviceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除楼栋设备中间信息
     *
     * @param id 楼栋设备中间ID
     * @return 结果
     */
    @Override
    public int deleteBuildingDeviceById(Long id)
    {
        return buildingDeviceMapper.deleteBuildingDeviceById(id);
    }

    @Override
    public int insertBuildingDevices(List<BuildingDevice> list) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        return buildingDeviceMapper.insertBuildingDevices(list);
    }

    @Override
    public List<BuildingDeviceDAO> selectBuildingDeviceListWithDeviceInfo(BuildingDeviceListSearch search) {
        if (CollectionUtil.isEmpty(search.getBuildingIds()) && StringUtils.isNull(search.getBuildingId())) {
            return new ArrayList<>();
        }
        return buildingDeviceMapper.selectBuildingDeviceListWithDeviceInfo(search);
    }

    @Override
    public List<BuildingDevice> selectBuildingDeviceListWithDevice(BuildingDeviceListSearch search) {
        QueryWrapper<BuildingDevice> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("building_id",search.getBuildingId());
        return buildingDeviceMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteBuildBuildingV1(Long id) {
        buildingDeviceMapper.deleteBuildingDeviceListWithDeviceInfo(id);
    }
}
